package com.tanhua.recommend.listener;

import com.tanhua.domain.mogo.VideoScore;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;

import java.util.Map;

@Slf4j
@RocketMQMessageListener(consumerGroup = "tanhua-video-group",topic = "tanhua-video")

public class VideoScoreListener implements RocketMQListener<Map> {
    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public void onMessage(Map map) {
        log.info("====接收视频推荐消息【开始】====");
        //获取参数
       Long userId= Long.valueOf(map.get("userId").toString());
       Long vid= Long.valueOf(map.get("vid").toString());
       Integer type=(Integer) map.get("type");
       //根据操作类型进行计分
        VideoScore videoScore = new VideoScore();
        videoScore.setUserId(userId);
        videoScore.setVideoId(vid);
        videoScore.setDate(System.currentTimeMillis());
        switch (type){
            case 1:
                videoScore.setScore(20D);
            case 2:
                videoScore.setScore(5d);
                break;
            case 3:
                videoScore.setScore(-5d);
                break;
            case 4:
                videoScore.setScore(10d);
                break;
            default:
        }
        mongoTemplate.save(videoScore);
        log.info("====接收视频推荐消息【完成】====");


    }
}
